package com.lan.workflow.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lan.workflow.common.ResponseCodeEnum;
import com.lan.workflow.service.IActivitiService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ActivitiServiceImpl implements IActivitiService {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private ObjectMapper objectMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String,Object> approval(String instanceId, Map<String, Object> variables, String userId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        /*Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipal() == null){
            result.put("code", ResponseCodeEnum.BUSINESS_ERROR.getKey());
            result.put("msg", "您没有权限审核");
        } else {
            AuthorizationInfo authorizationInfo = userRealm.getAuthorizationInfo(subject.getPrincipals());
            Collection<String> roles = authorizationInfo.getRoles();*/
            Collection<String> roles = Arrays.asList("bmjl","rs");
            boolean hasAuditPermissions = false;
            Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
            if(StringUtils.isEmpty(task.getAssignee())){
                List<IdentityLink> identityLinkList = taskService.getIdentityLinksForTask(task.getId());
                for(IdentityLink identityLink : identityLinkList){
                    if("candidate".equals(identityLink.getType())){
                        if(identityLink.getGroupId() != null && roles.contains(identityLink.getGroupId())){
                            hasAuditPermissions = true;
                            break;
                        }
                        if(userId.equals(identityLink.getUserId())){
                            hasAuditPermissions = true;
                            break;
                        }
                    }
                }
                if(hasAuditPermissions){
                    taskService.claim(task.getId(), userId);
                }
            } else {
                if(userId.equals(task.getAssignee())){
                    hasAuditPermissions = true;
                } else {
                    hasAuditPermissions = false;
                }
            }
            if(hasAuditPermissions){
                Authentication.setAuthenticatedUserId(userId);
                // 添加批注信息
                taskService.addComment(task.getId(), task.getProcessInstanceId(), objectMapper.writeValueAsString(variables));
                taskService.complete(task.getId(), variables);
                result.put("code", ResponseCodeEnum.SUCCESS.getKey());
                result.put("msg", ResponseCodeEnum.SUCCESS.getValue());
            } else {
                result.put("code", ResponseCodeEnum.BUSINESS_ERROR.getKey());
                result.put("msg", "您没有权限审核");
            }
        //}
        return result;
    }
}
